<template>
	<view class="content">
		<view class="logo">
			<image src="../../static/img/gongLog.png" mode=""></image>
			<view>干工程</view>
		</view>
		
		
		<view class="uni-form-item uni-column">
			<input type="tel" class="uni-input" name="name" placeholder="请输入用户名" v-model="name" />
		</view>
		<view class="uni-form-item uni-column">
			<input type="password" class="uni-input" name="pwd" placeholder="请输入密码" v-model="pwd" />
		</view>
		<button type="primary" @click="sublim">登陆</button>
		<view class="links">
			<view @tap="gotoForgetPassword">忘记密码？</view>
			<view>|</view>
			<view class="link-highlight" @tap="gotoRegistration">注册账号</view>
		</view>
	</view>
</template>




<script>
	var that;
	export default {
		data() {
			return {
				name: "admin",
				pwd: "123456"
			}
		},
		onLoad() {
			that = this;
			this.loadExecution();
			 const jsee =  uni.getStorageSync("JSESSIONID"); 
			 console.log("login -> JSESSIONID="+jsee)
			 /* if(jsee!=null || jsee!=""){
				 uni.switchTab({url:'../index/index'});
			 } */

		},
		methods: {
			loadExecution: function(){
				/**
				 * 获取本地存储中launchFlag的值
				 * 若存在，说明不是首次启动，直接进入首页；
				 * 若不存在，说明是首次启动，进入引导页；
				 */
				try {
				    const value = uni.getStorageSync('launchFlag');
					
					console.log(value)
				    if (value) {
				        if (value == true) {				
							//判断是否登录
							// _this.isNoLogin();
				            uni.redirectTo({
				                url: '/pages/login/login'
				            }); 
				        } else {
				            uni.redirectTo({
				                url: '/pages/index/guide'
				            });
				        }
				    } else {
				        uni.setStorageSync('launchFlag',true);
				        uni.redirectTo({
				            url: '/pages/index/guide'
				        });
				    }
				}
				 catch(e) { 
					// error 
					uni.setStorage({ 
						key: 'launchFlag', 
						data: true, 
						success: function () {
							console.log('error时存储launchFlag');
						} 
					}); 
					uni.redirectTo({ url: '/pages/index/guide' }); 
				}
				/* return;
				uni.switchTab({
				    url: '/pages/index/index'
				}); */
			},
			gotoRegistration: function() {
				uni.navigateTo({
					url: 'registration'
				});
			},
			gotoForgetPassword: function() {
				uni.navigateTo({
					url: 'forget-password'
				});
			},
			sublim: function() {
				if (that.name != "" && that.pwd != "") {
					console.log(that.name + "," + that.pwd);
			        //请求登录,将信息保存到userInfo
					//存储请求ip和端口号,不然修改麻烦 @陈远
					 uni.setStorageSync("pathHost","http://localhost:8088");
					 const val = uni.getStorageSync("pathHost");
					 uni.request({
						url: val+'/ggcapp/login',
						responseType:'json',
						method:'POST',
						dataType:"json",
						header: {
						       'content-type': 'application/x-www-form-urlencoded' 
						},
						data:{
							__ajax:"",
							username:that.name,
							password:that.pwd,
							mobileLogin:true
						},
						success:function(datat){
								//console.log("登录成功："+JSON.stringify(datat));
								if(datat.data.success){ //登录成功
									//保存用户信息到缓存
									datat.data.body.pwd=that.pwd;
									//loginState:false //是否登录的状态 --是否可以获取数据
									uni.setStorageSync("loginState",true);
									//保存JSESSIONID到缓存,每次请求都要带
									uni.setStorageSync("JSESSIONID",datat.data.body.JSESSIONID); 
									uni.request({
									  	 url: val+"/ggcapp/sys/user/infoData"+";JSESSIONID="+datat.data.body.JSESSIONID+"?__ajax=true&mobileLogin=true",
									  	 success(data) {
											//console.log(data)
											datat.data.body.photo=val+""+data.data.body.data.photo;
											datat.data.body.no=data.data.body.data.no;
											datat.data.body.mobile=data.data.body.data.mobile;
											datat.data.body.roleNames=data.data.body.data.roleNames; //角色
											datat.data.body.companyName=data.data.body.data.company.name;
											//console.log(datat.data)
											//保存用户信息到缓存
											uni.setStorageSync("userInfo",datat); 
										}
									});
			                       //获取全部用户角色 --存入缓存
								   uni.request({
								     	 url: val+"/ggcapp/sys/role/getRoleByUserNow"+";JSESSIONID="+datat.data.body.JSESSIONID+"?__ajax=true&mobileLogin=true",
								     	 success(data) {
								   		     //console.log(data.data)
								   	         uni.setStorageSync("userRoleAll",data.data); 
								   		
								     	}
								   });
			
			
			
			
									//保存点击项目的id(模拟--用与用户点击通讯录里的组织机构显示项目通行录)
									// uni.setStorageSync("projectId","cb1dfce83ea84299982cae5bd2bfc87d");
									//登陆极光im
									/* console.log("---------jim--------")
						
									that.$jjim.config(that.name,that.pwd);
									that.$jjim.login().then(function(dat){
										console.log(dat)
									},function(error){
										console.log(error)
									}); */
									// console.log("---------jim--------")
									
									
									
									 const value = uni.getStorageSync('JSESSIONID');
									 console.log("JSESSIONID:"+value)
									uni.switchTab({url:'../index/indexNew'});
								}else{
									//loginState:false //是否登录的状态 --是否可以获取数据
									uni.setStorageSync("loginState",false);
									uni.showToast({
										title: ''+datat.data.msg,
										duration: 2000,
										icon: "none"
									});
								}
								
								
						}
					}) 
					

				} else {
					uni.showToast({
						title: '请输入用户名或密码',
						duration: 2000,
						icon: "none"
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #b49950;

	.content {
		padding: 100upx;
	}

	.logo {
		text-align: center;
        position: relative;
		margin-bottom: 15%;
		image {
			height: 200upx;
			width: 200upx;
			margin: 0 0 60upx;
		}
		view {
			position: absolute;
			bottom: 20upx;
			left: 42%;
		}
	}

	.uni-form-item {
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;

		.uni-input {
			font-size: 30upx;
			//padding: 7px 0;
		}
	}

	button[type="primary"] {
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}

	.links {
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;

		view {
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}

		.link-highlight {
			color: $color-primary
		}
	}
</style>
